<?php 
defined('RUN_KATANA') or die('Restricted access');
class SitemapItem extends Object
{
	public $ParentId;
	public $SiteId;
	public $IsFrontpage;
	public $HasSubItems;
	public $Title;
	public $Alias;
	public $PageType;
	public $Description;
	
	public $Link;
	public $LinkHTML;
	public $LinkType;
	public $LinkId;
	public $LinkUrl;
	public $LinkTarget;
	
	public $Active; 	
	public $Visible;
	public $Access; 
	public $MenuOrder;
	public $ThemeId;
	public $LayoutId;
	public $Level;
	public $SubItems;
	public $SubLoaded;
	public $DataChanged;
	
	public $StaticHtmlId;
	public $StaticHtml;
	public $FormId;
	public $SearchIndex;
	
	public $Image;
	public $ImageObject;
	
	public function SitemapItem($values = '') 
	{ 
		self::setDataTablePrefix('sitemap_');
		self::setDataTable('pages');
		
		if(is_array($values))
		{ self::setValues($values); }
		
		self::setSubLoaded(false);
		self::setDataChanged(false);
	}
	
	public function getParentId() { return intval($this->ParentId); }
	public function getSiteId() { return intval($this->SiteId); }
	public function getIsFrontpage() { return intval($this->IsFrontpage); }
	public function getHasSubItems() { return intval($this->HasSubItems); }
	public function getLinkType() { return intval($this->LinkType); } 	
	public function getLinkId() { return intval($this->LinkId); } 	
	public function getLinkTarget() { return intval($this->LinkTarget); }
	public function getActive() { return intval($this->Active); } 	
	public function getVisible() { return intval($this->Visible); } 	
	public function getAccess() { return intval($this->Access); } 
	public function getMenuOrder() { return intval($this->MenuOrder); }
	public function getThemeId() { return intval($this->ThemeId); }
	public function getLayoutId() { return intval($this->LayoutId); }
	
	public function getSubItems() { return $this->SubItems; }
	public function getSubLoaded() { return $this->SubLoaded; }
	public function getDataChanged() { return $this->DataChanged; }
	public function getStaticHtml() { return $this->StaticHtml; }
	
	public function getImageURL() { return PAGE_URL.self::getImage(); }
	public function getImageThumbURL() { return PAGE_URL.'thumbs/'.self::getImage(); }
	
	public function getLink()
	{ 
		if($this->Link != '')
		{ return $this->Link; }
		switch(self::getLinkType())
		{
			case 0:
			case 10:
				if(self::getIsFrontpage())
				{ $this->Link = SITE_URL; }
				else
				{ $this->Link = SITE_URL.'m/'.self::getId().'/'; }
			break;
			case 1:
				$this->Link = self::getLinkUrl(); 
			break;
			case 2:
				$this->Link = SITE_URL.'c/'.self::getLinkId().'/'; 
			break;
			default:
				$this->Link = SITE_URL.'m/'.self::getId().'/';
			break;
		}
		return $this->Link;
	}
	
	public function getLinkHTML($class='', $params = array())
	{ 
		if($this->LinkHTML != '')
		{ return $this->LinkHTML; }
		
		if(self::getId() == $_GET['m'])
		{ $class .= ' active'; }
		if((self::getLinkUrl() != '') && ($_GET['m'] > 0))
		{
			if(stripos(self::getLinkUrl(),'/m/'.$_GET['m'].'/') > -1)
			{ $class .= ' active'; }
		}
		if((self::getLinkType() == 2) && ($_GET['c'] == self::getLinkId()))
		{ $class .= ' active'; }
		
		$this->LinkHTML = '<a class="'.$class.' type_'.self::getLinkType().'" ';
		if(count($params) > 0)
		{
			foreach(array_keys($params) as $param)
			{ $this->LinkHTML .= $param.'="'.$params[$param].'" '; }
		}
		if(self::getLinkTarget() == 1)
		{ $this->LinkHTML .= ' target="_blank" '; }
		$this->LinkHTML .= ' href="'.self::getLink().'" ';
		$this->LinkHTML .= '>'.self::getTitle().'</a>';
		
		return $this->LinkHTML;
	}
	
	public function getPageType()
	{ 
		if($this->PageType != '')
		{ return $this->PageType; }
		switch(self::getLinkType())
		{
			case 0:
			case 10:
				$this->PageType = 'page';
			break;
			case 1:
				$this->PageType = 'extlink';
			break;
			case 2:
				$this->PageType = 'conponent';
			break;
			case 5:
				$this->PageType = 'article';
			break;
			default:
				$this->PageType = 'page';
			break;
		}
		return $this->PageType;
	}
	
	/* Set Functions */
	public function setParentId($val) { $this->ParentId=intval($val); }
	public function setSiteId($val) { $this->SiteId=intval($val); }
	public function setIsFrontpage($val) { $this->IsFrontpage=intval($val); }
	public function setHasSubItems($val) { $this->HasSubItems=intval($val); }
	public function setLinkType($val) { $this->LinkType=intval($val); }
	public function setLinkId($val) { $this->LinkId=intval($val); }
	public function setLinkTarget($val) { $this->LinkTarget=intval($val); }
	public function setActive($val) { $this->Active=intval($val); }
	public function setVisible($val) { $this->Visible=intval($val); }
	public function setAccess($val) { $this->Access=intval($val); }
	public function setMenuOrder($val) { $this->MenuOrder=intval($val); }
	public function setThemeId($val) { $this->ThemeId=intval($val); }
	public function setLayoutId($val) { $this->LayoutId=intval($val); }
	
	public function setSubItems($val) { $this->SubItems=$val; }
	public function unsetSubItems() { unset($this->SubItems); }
	public function setSubLoaded($val) { $this->SubLoaded=$val; }
	public function setDataChanged($val) { $this->DataChanged=$val; }
	/* Set Functions End */
	
	public function addToSearchIndex($val) { $this->SearchIndex=$this->SearchIndex.$val; }
	
	public function loadMetaData()
	{
		global $katana;
		$katana->setMetaTitle(self::getMetaTitle());
		$katana->addAfterMetaTitle($katana->getTitle());
		$katana->setMetaData(self::getMetaDesc());
		$katana->setMetaKey(self::getMetaKey());
		
		$katana->addHeaderItem('<meta itemprop="name" content="'.self::getTitle().'" >');
		$katana->addHeaderItem('<meta itemprop="description" content="'.self::getMetaDesc().'" >');
		
		if(self::getPageType() == 'article')
		{ $katana->addHeaderItem('<meta property="og:type" content="article" >'); }
		$katana->addHeaderItem('<meta property="og:title" content="'.self::getTitle().'" >');
		$katana->addHeaderItem('<meta property="og:description" content="'.self::getMetaDesc().'" >');
		if(self::getImage() != '')
		{ 
			$katana->addHeaderItem('<meta itemprop="image" content="'.self::getImageURL().'" >');	
			$katana->addHeaderItem('<meta property="og:image" content="'.self::getImageURL().'" >'); 
		}
		$katana->addHeaderItem('<meta property="og:url" content="'.self::getLink().'" >');
	}
	
	public function update()
	{
		if($this->Id > 0)
		{
			$sql  = 'UPDATE '.DB_PERFIX.self::getDataTablePrefix().self::getDataTable().' SET ';
			$sql .= '`parent_id`='.self::getParentId().', ';
			$sql .= '`site_id`='.self::getSiteId().', ';
			if($this->IsFrontpage == true)
			{ $sql .= '`frontpage`=1, '; }
			else
			{ $sql .= '`frontpage`=0, '; }
			if($this->SubLoaded)
			{ $sql .= '`subitems`='.count($this->SubItems).', '; }
			$sql .= '`title`=\''.mysql_real_escape_string(self::getTitle()).'\', ';
			$sql .= '`Image`=\''.mysql_real_escape_string(self::getImage()).'\', '; 
			$sql .= '`Description`=\''.mysql_real_escape_string(self::getDescription()).'\', ';
			$sql .= '`alias`=\''.mysql_real_escape_string(self::getAlias()).'\', '; 	
			$sql .= '`link_type`='.self::getLinkType().', '; 	
			$sql .= '`link_id`='.self::getLinkId().', '; 	
			$sql .= '`link_url`=\''.mysql_real_escape_string(self::getLinkUrl()).'\', '; 	
			$sql .= '`link_target`=\''.mysql_real_escape_string(self::getLinkTarget()).'\', '; 	
			$sql .= '`visible`='.self::getVisible().', '; 	
			$sql .= '`access`='.self::getAccess().', '; 	
			$sql .= '`menu_order`='.self::getMenuOrder().', '; 	
			$sql .= '`theme_id`='.self::getThemeId().', '; 	
			$sql .= '`layout_id`='.self::getLayoutId().', ';
			
			$sql .= '`MetaTitle`=\''.mysql_real_escape_string(self::getMetaTitle()).'\', ';
			$sql .= '`meta_desc`=\''.mysql_real_escape_string(self::getMetaDesc()).'\', ';
			$sql .= '`meta_key`=\''.mysql_real_escape_string(self::getMetaKey()).'\', ';
			
			$sql .= '`active`='.self::getActive().' ';
			$sql .= 'WHERE `id`='.self::getId().';';
			$result = mysql_query($sql);
			
			if(self::getStaticHtmlId() > 0)
			{
				$sql   = 'UPDATE '.DB_PERFIX.self::getDataTablePrefix().'static_html SET ';
				$sql  .= '`html`=\''.mysql_real_escape_string(self::getStaticHtml()).'\', ';
				$sql  .= '`FormId`= '.self::getFormId().' ';
				$sql  .=  'WHERE id='.self::getStaticHtmlId().';';
				$result = mysql_query($sql);
			}
			else
			{
				if(self::getStaticHtml() != '')
				{
					$sql = 'INSERT INTO '.DB_PERFIX.self::getDataTablePrefix().'static_html ';
					$sql .= '(`parent_id`, `html`, `FormId`) ';
					$sql .= 'VALUES ';
					$sql .= '('.self::getId().', \''.mysql_real_escape_string(self::getStaticHtml()).'\', '.intval(self::getFormId()).');';
					$result = mysql_query($sql);
				}
			}
			if(self::getSearchIndex() != '')
			{
				$sql = 'DELETE FROM '.DB_PERFIX.self::getDataTablePrefix().'search_index WHERE parent_id='.self::getId().';';
				$result = mysql_query($sql);
				$sql = 'INSERT INTO '.DB_PERFIX.self::getDataTablePrefix().'search_index ';
				$sql .= '(`parent_id`, `content`) ';
				$sql .= 'VALUES ';
				$sql .= '('.self::getId().', "'.self::getSearchIndex().'");';
				$result = mysql_query($sql);
			}
		}
	}
	
	public function loadSubItems() 
	{
		if((self::getId() > 0) && ($this->SubLoaded == false) && ($this->HasSubItems))
		{
			global $db;
			$sql  = ' SELECT * FROM '.DB_PERFIX.self::getDataTablePrefix().self::getDataTable().' WHERE parent_id='.self::getId();
			$sql .= ' ORDER BY menu_order, id ASC;';
			
			$result = $db->query($sql);
			$data = $db->fetch($sql, CACHE_TTL);
			
			if(is_array($data))
			{
				unset($this->SubItems);
				foreach($data as $row)
				{
					$this->SubItems[$row['id']] = new SitemapItem;
					$this->SubItems[$row['id']]->setId($row['id']);
					$this->SubItems[$row['id']]->setParentId($row['parent_id']);
					$this->SubItems[$row['id']]->setSiteId($row['site_id']);
					$this->SubItems[$row['id']]->setIsFrontpage($row['frontpage']);
					$this->SubItems[$row['id']]->setHasSubItems($row['subitems']);
					$this->SubItems[$row['id']]->setTitle($row['title']);
					$this->SubItems[$row['id']]->setImage($row['Image']);
					$this->SubItems[$row['id']]->setDescription($row['Description']);
					$this->SubItems[$row['id']]->setAlias($row['alias']);
					$this->SubItems[$row['id']]->setLinkType($row['link_type']);
					$this->SubItems[$row['id']]->setLinkId($row['link_id']); 
					$this->SubItems[$row['id']]->setLinkUrl($row['link_url']);
					$this->SubItems[$row['id']]->setLinkTarget($row['link_target']);
					$this->SubItems[$row['id']]->setActive($row['active']);
					$this->SubItems[$row['id']]->setVisible($row['visible']);
					$this->SubItems[$row['id']]->setAccess($row['access']);
					$this->SubItems[$row['id']]->setMenuOrder($row['menu_order']);
					$this->SubItems[$row['id']]->setThemeId($row['theme_id']);
					$this->SubItems[$row['id']]->setLayoutId($row['layout_id']);
					$this->SubItems[$row['id']]->setMetaTitle($row['MetaTitle']);
					$this->SubItems[$row['id']]->setMetaDesc($row['meta_desc']);
					$this->SubItems[$row['id']]->setMetaKey($row['meta_key']);
				}
			}
		}
	}
	
	public function loadDetails() 
	{
		if((self::getId() > 0))
		{
			$sql  = ' SELECT * FROM '.DB_PERFIX.self::getDataTablePrefix().'static_html WHERE parent_id='.self::getId().';';
			$result = mysql_query($sql); 
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{ 
					self::setStaticHtml($row['html']); 
					self::setStaticHtmlId($row['id']); 
					self::setFormId($row['FormId']); 
				}
			}
			$sql  = ' SELECT content FROM '.DB_PERFIX.self::getDataTablePrefix().'search_index WHERE parent_id='.self::getId().';';
			$result = mysql_query($sql); 
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{ self::setSearchIndex($row['content']); }
			}
		}
	}
	
	function uploadImage($file)
	{
		global $thumb;
		self::setImage(self::uploadObjectImage(PAGE_PATH, $file, $thumb['DEFAULT']));
		$this->ImageObject = new Image();
		$this->ImageObject->setFolderPath(PAGE_PATH);
		$this->ImageObject->setFolderUrl(PAGE_URL);
		$this->ImageObject->setFilename($this->Image);
	}
	
	function deleteImage()
	{
		global $thumb;
		self::setImage(self::deleteObjectImage(PAGE_PATH, self::getImage(), $thumb['DEFAULT']));
		unset($this->ImageObject);
	}
	
	function getParseFeed($max=50)
	{
		if(self::getLinkUrl() != '')
		{
			if(is_file(CACHE_PATH.'feed_'.self::getId().'.xml'))
			{ $xmlStr = file_get_contents(CACHE_PATH.'feed_'.self::getId().'.xml'); }
			else
			{ 
				$xmlStr = file_get_contents(self::getLinkUrl()); 
				if($xmlStr)
				{ file_put_contents(CACHE_PATH.'feed_'.self::getId().'.xml', $xmlStr); }
			}
			
			//$xmlStr = file_get_contents(self::getLinkUrl());
			if($xmlStr)
			{
				file_put_contents(CACHE_PATH.'feed_'.self::getId().'.xml', $xmlStr);
				$xmlDoc = new DOMDocument();
				
				if($xmlDoc->loadXML($xmlStr, LIBXML_NOERROR))
				{
					/*
					$channel = $xmlDoc->getElementsByTagName('channel')->item(0);
				
					
					$rssfeed['ch_title'] = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
					$rssfeed['ch_link'] = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
					$rssfeed['ch_description'] = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
					  */
					$x=$xmlDoc->getElementsByTagName('item');
					for ($i=0; $i < $max; $i++)
					{ 
						if($x->item($i)) 
						{
							$rssfeed[$i]['title'] 			= $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
							$rssfeed[$i]['link'] 			= $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
							if(is_object($x->item($i)->getElementsByTagName('description')))
							{ $rssfeed[$i]['description'] 	= $x->item($i)->getElementsByTagName('description')->item(0)->nodeValue; }
							
							if(is_object($x->item($i)->getElementsByTagName('uvod')))
							{ $rssfeed[$i]['intro'] 	= $x->item($i)->getElementsByTagName('uvod')->item(0)->nodeValue; }
							
							if(is_object($x->item($i)->getElementsByTagName('clanak')))
							{ 
								$rssfeed[$i]['article'] 	= $x->item($i)->getElementsByTagName('clanak')->item(0)->nodeValue; 
								$rssfeed[$i]['article'] = nl2br($rssfeed[$i]['article']);
							}
							
							if(is_object($x->item($i)->getElementsByTagName('image')))
							{ $rssfeed[$i]['image'] 	= $x->item($i)->getElementsByTagName('image')->item(0)->nodeValue; }
							
							if(is_object($x->item($i)->getElementsByTagName('izvor')))
							{ $rssfeed[$i]['source'] 	= $x->item($i)->getElementsByTagName('izvor')->item(0)->nodeValue; }
							
							if($rssfeed[$i]['title'] != '')
							{ $rssfeed[$i]['title'] = trim($rssfeed[$i]['title']); }
						}
					}
					return $rssfeed;
				}
				else
				{ return; }
			}
		}
	}
}
?>